package com.sds.annotation.processor;

import java.util.Collection;
import java.util.Map;

import com.sun.mirror.apt.AnnotationProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.declaration.AnnotationMirror;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import com.sun.mirror.declaration.AnnotationTypeElementDeclaration;
import com.sun.mirror.declaration.AnnotationValue;
import com.sun.mirror.declaration.Declaration;
import com.sun.mirror.util.SourcePosition;

@SuppressWarnings("deprecation")
public class LengthProcessor implements AnnotationProcessor {

	private AnnotationProcessorEnvironment env;
	private AnnotationTypeDeclaration length;
	
	public LengthProcessor(AnnotationProcessorEnvironment env) {
		this.env = env;
		length = (AnnotationTypeDeclaration) env.getTypeDeclaration("com.sds.annotation.type.Length");
	}
	
	@Override
	public void process() {
		Collection<Declaration> declarations = env.getDeclarationsAnnotatedWith(length);
		
		for (Declaration declaration : declarations) {
			this.processLength(declaration);
		}
	}
	
	private void processLength(Declaration declaration) {
		Collection<AnnotationMirror> annotations = declaration.getAnnotationMirrors();
		
		for (AnnotationMirror mirror : annotations) {
			if (mirror.getAnnotationType().getDeclaration().equals(length)) {
				SourcePosition sp = mirror.getPosition();
				Map<AnnotationTypeElementDeclaration, AnnotationValue> values = mirror.getElementValues();
				
				System.out.println("Declataion : " + declaration.toString());
				System.out.println("Position: "  + sp);
				System.out.println("Values: ");
				for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> entry : values.entrySet()) {
					AnnotationTypeElementDeclaration elemDecl = entry.getKey();
					AnnotationValue value = entry.getValue();
					System.out.println("\t" + elemDecl + "=" + value);
				}
			}
		}
	}

}
